<template>
  <div class="table-data-view" v-loading='tableLoading'>
    <div class="common-screen-view">
      <div class="common-screen-cell">
        <!-- <div class="common-screen-title"  style="width:65px">名称：</div> -->
        <el-input v-model="condition" :placeholder="$t('tables_title.name')"></el-input>
      </div>
      <el-button type="primary" icon="el-icon-search" @click="searchNotice">{{$t('global.search')}}</el-button>
      <el-button type="primary" icon="el-icon-plus" v-if="checkPerm('notice_add')" @click="addNotice">{{$t('global.add')}}</el-button>
    </div>
    <table-list class="table-list-view" :tableConfig="tableConfig" :tableData="tableData" @reviseNotice="reviseNotice" @delNotice="delNotice"></table-list>
    <table-pagination
      :total="count"
      :currentPage="pageNow"
      @handleSizeChange="handleSizeChange"
      @handleCurrentChange="handleCurrentChange">
    </table-pagination>

    <el-dialog :title="$t('notice_management.edit_message_info')" :close-on-click-modal="false" :visible.sync="noticeInfoVisible" width="800px">
      <add-edit-notice :editNoticeInfo="editNoticeInfo" @refreshData="refreshData" @cancelView="cancelView"></add-edit-notice>
    </el-dialog>
  </div>
</template>

<script>

import { checkPermission } from '@/common/utils/permission.js'
import { getNoticeList,deleteNotice } from '@/api/noticeApi.js'

export default {
  data(){
    return{
      tableLoading:false,
      roleInfoVisible:false,
      isEdit:false,
      noticeInfoVisible:false,
      tableConfig:{},       //table的基本配置
      tableData:[],         //table数据源
      total: 0,             //数据总数
      pageNow:1,        //当前页码
      condition:'',
      count:0,
      pageSize:10,      //每页查询数量
      editNoticeInfo:{},
    }
  },
  components:{
    tableList: () => import("@/components/common_tables.vue"),            //引入table组件
    tablePagination: () => import("@/components/table_pagination.vue"),    //引入页码组件   为方便拓展 把它与table组件分开封装
    addEditNotice:()=>import("./add_edit_notice.vue")
  },
  created(){
    this.tableConfig = {
      isCheckBox:true,
      props:[
        { type: "text", label: "tables_title.title", prop: "title",width:220},
        { type: "v-text", label: "tables_title.content", prop: "content"},
        { type: "text", label: "tables_title.creater_name", prop: "createrName",width:220},
        { type: "text", label: "tables_title.create_time", prop: "createTime",width:220},
      ],
      isOpt:true,
      optBtns:[
        {label:'global.modify',callback:'reviseNotice',code:'notice_update'},
        {label:'global.delete',callback:'delNotice',code:'notice_delete'}]
    }
    this.getNoticeInfoList()
  },
  methods:{
    searchNotice(){
      this.pageNow = 1
      this.getNoticeInfoList()
    },
    getNoticeInfoList(){        //获取职位列表
      this.tableLoading = true
      getNoticeList({
      condition:this.condition,
      page:this.pageNow,
      limit:this.pageSize
      }).then((res)=>{
        this.tableLoading = false
        if(res.status == 200){
          this.count = res.data.count
          res.data.data.forEach(element => {
            element.state = element.status == 'ENABLE'
          });
          this.tableData = res.data.data
        }else{
          this.$message.error(res.data.message)
        }
      }).catch((e)=>{

      })
    },
    checkPerm(code){        //校验权限
      return checkPermission(code)
    },
    handleSizeChange(val){      //修改每页的显示条数
      this.pageSize = val
      this.pageNow = 1
      this.getNoticeInfoList()
    },
    handleCurrentChange(val){   //切换当前页码
      this.pageNow = val
    },
    addNotice(){          //新增通知
      this.editNoticeInfo = {}
      this.noticeInfoVisible = true
    },
    reviseNotice(row){        //修改通知信息
      this.editNoticeInfo = row
      this.noticeInfoVisible = true
    },
    delNotice(row){           //删除部门
      this.$confirm('是否删除通知"' + row.title + '"?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deleteNotice({noticeId:row.noticeId}).then((res)=>{
          console.log(res)
          if(res.data.code == 200){
            this.$message({
              message: '删除成功',
              type: 'success'
            });
            this.pageNow = 1
            this.getNoticeInfoList()
          }else{
            this.$message.error(res.data.message)
          }
        })
      }).catch(() => {

      });
    },
    cancelView(){       //关闭编辑弹窗
      this.noticeInfoVisible = false
    },
    refreshData(){      //刷新表格数据
      this.noticeInfoVisible = false
      this.page = 1
      this.getNoticeInfoList()
    },
  }
}
</script>

<style lang="scss" scoped>

</style>
